package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SysFault;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysFaultPmsMapper;
import com.ruoyi.system.domain.SysFaultPms;
import com.ruoyi.system.service.ISysFaultPmsService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.Validator;

/**
 * PMS缺陷信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-01
 */
@Service
public class SysFaultPmsServiceImpl implements ISysFaultPmsService
{
    private static final Logger log = LoggerFactory.getLogger(SysFaultPmsServiceImpl.class);

    @Autowired
    private SysFaultPmsMapper sysFaultPmsMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询PMS缺陷信息
     * 
     * @param faultId PMS缺陷信息主键
     * @return PMS缺陷信息
     */
    @Override
    public SysFaultPms selectSysFaultPmsByFaultId(Long faultId)
    {
        return sysFaultPmsMapper.selectSysFaultPmsByFaultId(faultId);
    }

    /**
     * 查询PMS缺陷信息列表
     * 
     * @param sysFaultPms PMS缺陷信息
     * @return PMS缺陷信息
     */
    @Override
    public List<SysFaultPms> selectSysFaultPmsList(SysFaultPms sysFaultPms)
    {
        return sysFaultPmsMapper.selectSysFaultPmsList(sysFaultPms);
    }

    /**
     * 新增PMS缺陷信息
     * 
     * @param sysFaultPms PMS缺陷信息
     * @return 结果
     */
    @Override
    public int insertSysFaultPms(SysFaultPms sysFaultPms)
    {
        sysFaultPms.setCreateTime(DateUtils.getNowDate());
        return sysFaultPmsMapper.insertSysFaultPms(sysFaultPms);
    }

    /**
     * 修改PMS缺陷信息
     * 
     * @param sysFaultPms PMS缺陷信息
     * @return 结果
     */
    @Override
    public int updateSysFaultPms(SysFaultPms sysFaultPms)
    {
        sysFaultPms.setUpdateTime(DateUtils.getNowDate());
        return sysFaultPmsMapper.updateSysFaultPms(sysFaultPms);
    }

    /**
     * 批量删除PMS缺陷信息
     * 
     * @param faultIds 需要删除的PMS缺陷信息主键
     * @return 结果
     */
    @Override
    public int deleteSysFaultPmsByFaultIds(String faultIds)
    {
        return sysFaultPmsMapper.deleteSysFaultPmsByFaultIds(Convert.toStrArray(faultIds));
    }

    /**
     * 删除PMS缺陷信息信息
     * 
     * @param faultId PMS缺陷信息主键
     * @return 结果
     */
    @Override
    public int deleteSysFaultPmsByFaultId(Long faultId)
    {
        return sysFaultPmsMapper.deleteSysFaultPmsByFaultId(faultId);
    }

    @Override
    public String importSysFaultPms(List<SysFaultPms> list, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysFaultPms obj : list)
        {
            try
            {
                // 验证是否存在这个公司
                SysFaultPms c = sysFaultPmsMapper.selectSysFaultPmsByFaultId(obj.getFaultId());
                if (StringUtils.isNull(c))
                {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setCreateBy(operName);
                    this.insertSysFaultPms(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、缺陷 " +obj.getFaultGroup() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setUpdateBy(operName);
                    this.updateSysFaultPms(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、缺陷 " +obj.getFaultGroup() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、缺陷 " +obj.getFaultGroup() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、缺陷 " +obj.getFaultGroup() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<SysFaultPms> selectSysFaultListByTime(Date overTime, Date startTime) {
        return sysFaultPmsMapper.selectSysFaultListByTime(overTime, startTime);
    }
}
